package com.ytjj.qmyx.supplychain.api.controller;

import com.ytjj.qmyx.supplychain.common.api.CommonPage;
import com.ytjj.qmyx.supplychain.common.api.CommonResult;
import com.ytjj.qmyx.supplychain.common.model.request.ProductCategoryPageListRequest;
import com.ytjj.qmyx.supplychain.common.model.response.ProductCategoryResponse;
import com.ytjj.qmyx.supplychain.mapper.entity.YxProductCategory;
import com.ytjj.qmyx.supplychain.service.ProductCategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 商品分类
 */
@RestController
@RequestMapping("/prodouctCategory")
public class ProductCategoryController {
    @Resource
    private ProductCategoryService productCategoryService;

    @GetMapping("/list")
    public CommonResult selectAll() {
        return CommonResult.success(productCategoryService.selectAll());
    }

    /**
     * 分页查询分类
     * @param request
     * @return
     */
    @PostMapping("/pageList")
    public CommonResult list(@RequestBody ProductCategoryPageListRequest request){
        CommonPage<YxProductCategory> pageList = productCategoryService.list(request);
        return CommonResult.success(pageList);
    }

    /**
     * 根据1级分类查询对应的分类信息
     * @return
     */
    @PostMapping("/getChildrensById")
    public CommonResult getChildrensById(@RequestBody ProductCategoryPageListRequest pcRequest){
        return CommonResult.success(productCategoryService.list(pcRequest));

    }

    /**
     * 新增分类表
     * @param productCategory
     * @return
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody YxProductCategory productCategory){
        int count = productCategoryService.create(productCategory);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 修改分类表
     * @param productCategory
     * @return
     */
    @PostMapping("/update")
    public CommonResult update(@RequestBody YxProductCategory productCategory){
        int count = productCategoryService.update(productCategory);
        if (count > 0) {
            //测试提交
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public CommonResult delete(@RequestParam Integer id){
        int count = productCategoryService.delete(id);
        if (count > 0) {
            return CommonResult.success(count);
        } else {
            return CommonResult.failed();
        }
    }

    /**
     * 根据分类id查询属性集合
     * @param categoryId
     * @return
     */
    @GetMapping("/getSpecListByCategoryId")
    public CommonResult getSpecListByCategoryId(@RequestParam Integer categoryId) {
        List<Map<String,Object>> specLists = productCategoryService.getSpecListByCategoryId(categoryId);
        return CommonResult.success(specLists);
    }

}
